# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=coreutils
pkgver=8.26
pkgrel=3
pkgdesc="The basic file, shell and text manipulation utilities of the GNU operating system"
arch=('i686' 'x86_64')
license=('GPL3')
url="https://www.gnu.org/software/coreutils"
groups=('base')
depends=('gmp' 'libiconv' 'libintl')
makedepends=('gmp-devel' 'libiconv-devel' 'gettext-devel' 'help2man')
install=${pkgname}.install
source=(https://ftp.gnu.org/gnu/${pkgname}/${pkgname}-${pkgver}.tar.xz{,.sig}
        001-coreutils-8.26-2.src.patch
        005-manifest.patch
        006-msys1-0x0d.patch
        008-msys2-src.patch
        009-msysize.patch)
sha256sums=('155e94d748f8e2bc327c66e0cbebdb8d6ab265d2f37c3c928f7bf6c3beba9a8e'
            'SKIP'
            'd9c159ed688c4399f1d952bef1d3cae7a23653a77d187e5dc788fe33c1b5816f'
            '22e73cf22f784657fae6161cc9538b2f0cd4043800df56c9f6b9aec4ec0a69ce'
            'dc501c52267f99cfd81bc00aa31b624183b189638feb43b54b326b62e52fc62a'
            'e411e3c8d202bab8c171082494ad6511dc8642933b367539e2f3006af0a67f7e'
            '46d30e057c86ff6448fee4b1664a9d8914d56d7efbfd97276212490a03926704')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p2 -i ${srcdir}/001-coreutils-8.26-2.src.patch
  patch -p1 -i ${srcdir}/005-manifest.patch
  patch -p1 -i ${srcdir}/006-msys1-0x0d.patch
  patch -p1 -i ${srcdir}/008-msys2-src.patch
  patch -p1 -i ${srcdir}/009-msysize.patch

  autoreconf -fi
  sed -i -e 's|\(libstdbuf\.so\)$(EXEEXT)|\1|g' Makefile.in
}

build() {
  [[ -d ${srcdir}/build-${CHOST} ]] && rm -rf ${srcdir}/build-${CHOST}
  mkdir -p ${srcdir}/build-${CHOST} && cd ${srcdir}/build-${CHOST}

  ../${pkgname}-${pkgver}/configure \
    --build=${CHOST} \
    --prefix=/usr \
    --libexecdir=/usr/lib \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --program-transform-name=s/kill/gkill/ \
    --without-libintl-prefix \
    --without-libiconv-prefix \
    --enable-install-program=arch,hostname \
    --enable-no-install-program=uptime

  make
}

package() {
  cd ${srcdir}/build-${CHOST}
  make DESTDIR=${pkgdir} install

  test ! -f ${pkgdir}/usr/lib/coreutils/libstdbuf.so.exe ||
  mv ${pkgdir}/usr/lib/coreutils/libstdbuf.so.exe \
    ${pkgdir}/usr/lib/coreutils/libstdbuf.so

  #mkdir -p ${pkgdir}/usr/etc
  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/src/dircolors.hin ${pkgdir}/etc/DIR_COLORS
}
